問題描述
為什麼 RichText 不能在 wagtail 管理員中為帖子工作?這是發生的事情的類型:
嘗試 post.content|richtext
(Why is RichText not working in wagtail admin for posts? This is the type of thing that happens:Trying post.content|richtext
)Wagtail 在現有 Django 站點上實現。我已經包含了最有可能包含錯誤的代碼部分。如果您還想要其他任何東西,請隨時提出要求。代碼如下:models.py
from django.conf import settings
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.urls import reverse
from django.utils.text import slugify
from wagtail.core.models import Page
from wagtail.core.fields import RichTextField
from wagtail.admin.edit_handlers import FieldPanel
class Subject(models.Model):
subject = models.CharField(max_length=200)
subject_slug = models.SlugField(editable=True, max_length=200, null=False, unique=True)
def get_absolute_url(self):
kwargs = {
'slug': self.slug
}
return reverse('subject', kwargs=kwargs)
def save(self, *args, **kwargs):
value = self.subject
self.slug = slugify(value, allow_unicode=True)
super().save(*args, **kwargs)
panels = [
FieldPanel('subject'),
FieldPanel('subject_slug')
]
class Meta:
# Gives the proper plural name for class called subject
verbose_name_plural = "Subjects"
def __str__(self):
return self.subject
class Post(models.Model):
author = models.ForeignKey(User, default=1, on_delete=models.SET_DEFAULT, related_name='blog_posts')
date_added = models.DateTimeField(auto_now_add=True)
subject = models.ForeignKey(Subject, verbose_name='Subject', default=1, on_delete=models.SET_DEFAULT)
title = models.CharField(max_length=200, unique=True)
slug = models.SlugField(editable=True, max_length=200, null=False, unique=True)
content = RichTextField()
def get_absolute_url(self):
kwargs = {
'slug': self.slug
}
return reverse('post_detail', kwargs=kwargs)
def save(self, *args, **kwargs):
value = self.title
self.slug = slugify(value, allow_unicode=True)
super().save(*args, **kwargs)
content_panels = Page.content_panels + [
FieldPanel('author'),
FieldPanel('subject'),
FieldPanel('title'),
FieldPanel('slug'),
FieldPanel('content')
]
class Meta:
ordering = ['‑date_added']
def __str__(self):
return self.title
class Comment(models.Model):
post = models.ForeignKey(Post, default=1, on_delete=models.SET_DEFAULT, related_name='comments')
name = models.CharField(max_length=80)
email = models.EmailField()
body = models.TextField()
made_on = models.DateTimeField(auto_now_add=True)
active = models.BooleanField(default=False)
class Meta:
ordering = ['made_on']
def __str__(self):
return 'Comment {} by {}'.format(self.body, self.name)
index.htm
{% extends 'blog/base.html' %}
{% load wagtailcore_tags %}
{% block content %}
{{ post.content|richtext }}
{% for post in posts %}
<div>
<h2><a href="{% url 'post_detail' post.subject.subject_slug post.slug %}">{{ post.title }}</a></h2>
<p>author: {{post.author}}, published: {{post.date_added }}</p>
<p>{{ post.content|linebreaksbr }}</p>
</div>
{% endfor %}
{% endblock %}
wagtail_hooks.py
from wagtail.contrib.modeladmin.options import (
ModelAdmin, ModelAdminGroup, modeladmin_register)
from .models import (
Subject, Post, Comment)
class SubjectAdmin(ModelAdmin):
model = Subject
menu_label = 'Subjects' from wagtail.contrib.modeladmin.options import (
ModelAdmin, ModelAdminGroup, modeladmin_register)
from .models import (
Subject, Post, Comment)
class SubjectAdmin(ModelAdmin):
model = Subject
menu_label = 'Subjects'
menu_icon = 'pilcrow'
list_display = ('subject', 'subject_slug')
list_filter = ('subject', 'subject_slug')
search_fields = ('subject', 'subject_slug')
prepopulated_fields = {'subject_slug': ['subject'],}
class PostAdmin(ModelAdmin):
model = Post
menu_label = 'Post'
menu_icon = 'author'
list_display = ('subject', 'title', 'slug','date_added', 'author')
list_filter = ('subject', 'date_added',)
search_fields = ('subject', 'title', 'content', 'author')
prepopulated_fields = {'slug': ['title'],}
modeladmin_register(SubjectAdmin)
modeladmin_register(PostAdmin)
menu_icon = 'pilcrow'
list_display = ('subject', 'subject_slug')
list_filter = ('subject', 'subject_slug')
search_fields = ('subject', 'subject_slug')
prepopulated_fields = {'subject_slug': ['subject'],}
class PostAdmin(ModelAdmin):
model = Post
menu_label = 'Post'
menu_icon = 'author'
list_display = ('subject', 'title', 'slug','date_added', 'author')
list_filter = ('subject', 'date_added',)
search_fields = ('subject', 'title', 'content', 'author')
prepopulated_fields = {'slug': ['title'],}
modeladmin_register(SubjectAdmin)
modeladmin_register(PostAdmin)
機器需要更多細節。除了我不想在我的帖子中說的以外,我不確定我還能說什麼:
Heading H2
Bold 而不是粗體
H3 粗體和斜體很有趣
參考解法
方法 1:
Thanks gasman, the way I fixed it was I implemented post.content|richtext and loaded wagtailcore_tags in templates and it works!